<!-- 登录日志管理 -->
<template>
  <div class="app-container">
    <!-- 列表 -->
    <el-col>
      <div v-if="isShowSearchBtn" class="search-container">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item label="关键字" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="用户名/昵称/手机号"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              class="!w-[240px]"
              v-model="dateTimeRange"
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="截止时间"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="resetQuery">
              <i-ep-refresh />
              重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <el-card shadow="never" class="table-container">
        <template #header>
          <div class="flex justify-between">
            <div>
              <el-button
                v-hasPerm="['loginlog@del']"
                type="danger"
                v-if="removeIds.length > 0"
                @click="handleDelete()"
                ><i-ep-delete />删除</el-button
              >
            </div>
            <div>
              <el-button
                class="ml-3"
                @click="handleSearchShowBtn"
                icon="Search"
                circle
              />
              <el-button
                class="ml-3"
                title="刷新"
                @click="handleQuery"
                icon="Refresh"
                circle
              />
            </div>
          </div>
        </template>

        <el-table
          v-loading="loading"
          :data="pageData"
          fit
          @selection-change="handleSelectionChange"
          @sort-change="handleSortChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="ID"
            prop="id"
            sortable="custom"
            align="center"
            width="80"
          >
            <template #default="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户名">
            <template #default="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="登录IP">
            <template #default="scope">
              <span>{{ scope.row.login_ip }}</span>
            </template>
          </el-table-column>
          <el-table-column label="IP归属">
            <template #default="scope">
              <span>{{ scope.row.ip_address }}</span>
            </template>
          </el-table-column>
          <el-table-column label="客户浏览器">
            <template #default="scope">
              <span>{{ scope.row.browser_type }}</span>
            </template>
          </el-table-column>
          <el-table-column label="系统" class-name="status-col">
            <template #default="scope">
              <span>{{ scope.row.system_type }}</span>
            </template>
          </el-table-column>
          <el-table-column label="登录时间" width="160">
            <template #default="scope">
              <span>{{ scope.row.created_at }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template #default="scope">
              <el-tooltip
                v-if="scope.row.status == 'error'"
                class="item"
                effect="dark"
                :content="scope.row.error_message"
                placement="top"
              >
                <el-tag type="danger" effect="dark"> 失败 </el-tag>
              </el-tooltip>
              <el-tag
                v-if="scope.row.status == 'success'"
                :type="scope.row.status"
                effect="dark"
              >
                成功
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right">
            <template #default="scope">
              <el-button
                v-hasPerm="['loginlog@del']"
                circle
                type="danger"
                title="删除"
                @click="handleDelete(scope.row.id)"
                ><i-ep-delete
              /></el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.p"
          v-model:limit="queryParams.limit"
          @pagination="handleQuery"
        />
      </el-card>
    </el-col>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "LoginLog",
  inheritAttrs: false,
});
import { fetchList, delLog } from "@/api/loginLog";
import { loginLog, loginLogQuery } from "@/api/loginLog/types";
const queryFormRef = ref(ElForm); // 查询表单
const isShowSearchBtn = ref(false);
const loading = ref(false); //  加载状态
const removeIds = ref([]); // 删除ID集合 用于批量删除
const queryParams = reactive<loginLogQuery>({
  p: 1,
  limit: 10,
  time: [] as any,
});
const dateTimeRange = ref([]);
const total = ref(0); // 数据总数
const pageData = ref<loginLog[]>(); // 分页数据

watch(dateTimeRange, (newVal) => {
  Object.assign(queryParams, { time: newVal });
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  fetchList(queryParams)
    .then(({ data }) => {
      pageData.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  dateTimeRange.value = [];
  queryParams.p = 1;
  queryParams.time = undefined;
  handleQuery();
}

/** 行选中 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 删除 */
function handleDelete(id?: number) {
  const ids = id || removeIds.value;
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    delLog({ ids: ids }).then(() => {
      handleQuery();
      ElMessage.success("删除成功");
    });
  });
}

/** 排序 */
function handleSortChange(data: any) {
  const { prop, order } = data;
  if (prop === "id") {
    if (order === "ascending") {
      queryParams.sort = "+id";
    } else {
      queryParams.sort = "-id";
    }
    handleQuery();
  }
}

//收起展开搜索按钮
function handleSearchShowBtn() {
  isShowSearchBtn.value = isShowSearchBtn.value === false;
}

onMounted(() => {
  handleQuery();
});
</script>
